package com.tianshi.pxx.service.impl;



import com.tianshi.pxx.mapper.UserMapper;
import com.tianshi.pxx.pojo.entity.User;
import com.tianshi.pxx.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;

@Service
@Slf4j
public class UserServiceImpl implements UserService {


    @Autowired
    private UserMapper userMapper;


    @Override
    public User login(User user) {
        User res = userMapper.selectByUsernameAndPassword(user);
        if(res!=null){
//            res.setLastLogin(LocalDateTime.now()); //更新最后登录时间
            res.setLastLogin(new Date()); //更新最后登录时间
            log.info("UserServiceImpl.login success{}",user);
            userMapper.update(res); //更新用户信息
        }
        return res;
    }

    @Override
    public Integer register(User user) {
//        user.setCreateTime(LocalDateTime.now()); //设置创建时间
        user.setCreateTime(new Date()); //设置创建时间
        user.setStatus(1);  //设置状态为可以正常登录
        log.info("UserServiceImpl.register success{}",user);
        return userMapper.insert(user);
    }

    @Override
    public User getUserById(Integer id) {
        return userMapper.selectById(id);
    }

    @Override
    public Integer updateUser(User user) {
        return userMapper.update(user);
    }
}
